package com.czmh.etc.onlinepubsys.controller.mingchuang;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.czmh.etc.onlinepubsys.entity.mingchuang.ExchangeCode;
import com.czmh.etc.onlinepubsys.json.mingchuang.JSONResult;
import com.czmh.etc.onlinepubsys.request.mingchuang.CreateCodeRequest;
import com.czmh.etc.onlinepubsys.request.mingchuang.ExchangeCodeRequest;
import com.czmh.etc.onlinepubsys.services.mingchuang.IExchangeCodeService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/{version}/api/mingchang/exchangeCode")
@Api(tags = "ETC订单接口【新】- 铭创")
public class ExchangeCodeController {
    @Value("${code.accessKey}")
    private String accessKey;

    @Autowired
    private IExchangeCodeService exchangeCodeService;

    @PostMapping("/createCode")
    public JSONResult createCode(@RequestBody CreateCodeRequest req) {
        log.info("生成兑换码:{}", JSON.toJSONString(req));
        JSONResult result = new JSONResult();
        if (!accessKey.equals(req.getAccessKey())) {
            result.setCode(JSONResult.FAIL);
            result.setMessage("已将你的信息通知网警");
            return result;
        }

        List<String> codes = exchangeCodeService.createCode(req.getDvId(), req.getNum());
        result.setRspData(JSON.toJSONString(codes));
        return result;
    }

    @PostMapping("/list")
    public JSONResult list(@RequestBody ExchangeCodeRequest req) {
        log.info("查询兑换码:{}", JSON.toJSONString(req));
        JSONResult result = new JSONResult();
        if (!accessKey.equals(req.getAccessKey())) {
            result.setCode(JSONResult.FAIL);
            result.setMessage("已将你的信息通知网警");
            return result;
        }
        IPage<ExchangeCode> page = exchangeCodeService.query(req);
        result.setRspData(JSON.toJSONString(page));
        return result;
    }
}
